<template>
  <div class="wh100">
    <el-form label-position="top" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="人员库名称：" prop="typeName" :rules="[
      {
        required: true,
        message: '人员库名称不能为空',
        trigger: 'blur',
      },
    ]">
            <el-input :disabled="modeType == 2" v-model="form.typeName"></el-input>
          </el-form-item>
        </el-col>

        <!--        <el-col :span="24">
          <el-form-item
            label="预警类型"
            prop="warningType"
            :rules="[
              {
                required: true,
                message: '请选择预警类型',
                trigger: 'blur',
              },
            ]"
          >
            <el-select v-model="form.warningType">
              <el-option
                v-for="item in warnList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>-->

        <!--        <el-col :span="12">
          <el-form-item label="是否预警" prop="isWarning" required>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-switch
                  :disabled="modeType == 2"
                  v-model="form.isWarning"
                ></el-switch>
                <span class="left10">预警</span>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>-->

        <el-col :span="24" style="text-align: right">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">
                <el-button type="primary" :loading="loading" @click="onSubmit('form')">保存</el-button>
              </div>
            </div>

          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { addSnapUser, editSnapUser } from "@/api/user";
export default {
  props: {
    modeType: {
      type: Number,
      default: 0,
    },
    snapUserInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  mounted() {
    if (this.modeType != 1) {
      this.form = JSON.parse(JSON.stringify(this.snapUserInfo));
      this.form.isWarning = this.form.isWarning == 1 ? true : false;
    }
  },
  data() {
    return {
      warnList: [
        {
          id: 0,
          name: "不预警",
        },
        {
          id: 1,
          name: "出现则预警",
        },
        {
          id: 2,
          name: "月末预警",
        },
      ],

      form: {
        typeName: "",
        isWarning: false,
        warningType: 1,
      },
      loading: false,
    };
  },

  methods: {
    async addSnapUser() {
      let res = await addSnapUser({
        typeName: this.form.typeName,
        isWarning: this.form.isWarning ? 1 : 0,
        warningType: this.form.warningType,
      });

      if (res.code == 200) {
        this.$message({
          message: "添加成功",
          type: "success",
        });

        this.loading = false;
        this.$emit("onClose", {
          isClose: true,
        });
      }
    },

    async editSnapUser() {
      let res = await editSnapUser({
        id: this.snapUserInfo.id,
        typeName: this.form.typeName,
        isWarning: this.form.isWarning ? 1 : 0,
        warningType: this.form.warningType,
      });

      if (res.code == 200) {
        this.$message({
          message: "修改成功",
          type: "success",
        });

        this.loading = false;
        this.$emit("onClose", {
          isClose: true,
        });
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.modeType == 1) {
            this.addSnapUser();
          } else if (this.modeType == 3) {
            this.editSnapUser();
          }
        }
      });
    },
    cancelModel() {
      this.$emit("onClose", {
        isClose: false,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.left10 {
  margin-left: 10px;
}

.btn {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 360px;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}
</style>
